package com.koron.plan.web.repairplan.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.plan.web.repairplan.bean.entity.PlanPackageDeptBean;
import com.koron.plan.web.repairplan.mapper.PlanPackageDeptMapper;
import com.koron.plan.web.repairplan.service.PlanPackageDeptService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author twcao
 * @title: PlanPackageDeptServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 主办单位service实现
 * @date 2021/9/1311:39
 */
@Service
public class PlanPackageDeptServiceImpl implements PlanPackageDeptService {

    /**
     * 保存或更新
     * @param factory 会话
     * @param beans   实例
     * @return
     */
    @Override
    public boolean saveOrUpdate(SessionFactory factory, List<PlanPackageDeptBean> beans) {
        Assert.notEmpty(beans, "主办单位集合不能为空");
        Assert.noNullElements(beans, "主办单位不能为空");
        PlanPackageDeptMapper deptMapper = factory.getMapper(PlanPackageDeptMapper.class);
        beans.stream().forEach(row -> row.setId(StringUtils.isBlank(row.getId()) ? CodeTools.getCode32() : row.getId()));
        return deptMapper.saveOrUpdate(beans);
    }

    /**
     * 根据计划包id删除主办部门
     * @param factory
     * @param packageId 计划包id
     * @return
     */
    @Override
    public boolean deleteByPackageId(SessionFactory factory, String packageId) {
        Assert.hasText(packageId, "计划包id不能为空");
        PlanPackageDeptMapper deptMapper = factory.getMapper(PlanPackageDeptMapper.class);
        return deptMapper.deleteByPackageId(packageId);
    }

    /**
     * 根据部门id获取部门详情
     * @param factory
     * @param deptId
     * @return
     */
    @Override
    public PlanPackageDeptBean getById(SessionFactory factory, String deptId) {
        PlanPackageDeptMapper deptMapper = factory.getMapper(PlanPackageDeptMapper.class);
        return deptMapper.queryById(deptId);
    }
}
